package com.lingxu.base.system.orgManage.service.impl;

import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.common.util.UUID32Generator;
import com.lingxu.base.system.manage.util.TreeNodeUtil;
import com.lingxu.base.system.orgManage.entity.SysUserMenu;
import com.lingxu.base.system.orgManage.mapper.SysUserMenuOldMapper;
import com.lingxu.base.system.orgManage.service.SysUserMenuOldService;
import com.lingxu.base.system.orgManage.vo.MenuTree;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Title: SysUserMenuServiceImpl
 * @Package: com.lingxu.base.system.orgManage.service.impl
 * @Author: ysj闫世杰
 * @Email: 1105324813@qq.com
 * @Date: Created in 2022/6/28 15:41
 * @Description:
 * @Version: 1.0.0
 */
@Service
public class SysUserMenuOldServiceImpl implements SysUserMenuOldService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysUserMenuOldMapper sysUserMenuMapper;
//    @Autowired
//    private SysOrgMenuMapper sysOrgMenuMapper;

    /**
     * @Author ysj闫世杰
     * @Description 查询未分配权限
     * @Date 2022/6/28 16:00
     * @Param: orgId
     * @return com.lingxu.base.common.api.vo.Result
     **/
//    @Override
//    public Result findNotExistsByOrgId(String orgId) {
//        try {
//            List<MenuTree> treeNodeList = new ArrayList<>();
//            List<MenuTree> rootMenu= sysOrgMenuMapper.findOrgNotExistMenu(orgId);
//            TreeNodeUtil treeUtil=new TreeNodeUtil<>();
//            treeNodeList.addAll(rootMenu);
//            List<MenuTree> result=treeUtil.list2TreeNode(treeNodeList);
//            return Result.ok(result,"查询成功");
//        }catch (Exception e){
//            logger.error("数据查询异常",e);
//            return Result.error("查询异常");
//        }
//    }

    /**
     * @Author ysj闫世杰
     * @Description 查询已分配权限
     * @Date 2022/6/28 16:46
     * @Param: orgId
     * @return com.lingxu.base.common.api.vo.Result
     **/
//    @Override
//    public Result findExistsByOrgId(String orgId) {
//        try {
//            List<MenuTree> treeNodeList = new ArrayList<>();
//            List<MenuTree> rootMenu= sysOrgMenuMapper.findOrgExistMenu(orgId);
//            TreeNodeUtil treeUtil=new TreeNodeUtil<>();
//            treeNodeList.addAll(rootMenu);
//            List<MenuTree> result=treeUtil.list2TreeNode(treeNodeList);
//            return Result.ok(result,"查询成功");
//        }catch (Exception e){
//            logger.error("数据查询异常",e);
//            return Result.error("查询异常");
//        }
//    }

    /**
     * @Author ysj闫世杰
     * @Description 单位绑定菜单
     * @Date 2022/6/28 16:55
     * @Param: orgMenus
     * @return com.lingxu.base.common.api.vo.Result
     **/
//    @Override
//    public Result addOrgBindMenu(List<SysOrgMenu> orgMenus) {
//        try {
//            Date date = new Date();
//            for(SysOrgMenu s : orgMenus){
//                s.setCreateTime(date);
//                //查询是否已存在菜单
//                int i = sysOrgMenuMapper.findParentExistMenu(s.getOrgId(),s.getMenuId());
//                if(i > 0){
//                    continue;
//                }
//                sysOrgMenuMapper.addRoleMenuBind(s);
//            }
//            return Result.ok("操作成功");
//        }catch (Exception e){
//            logger.error("操作异常",e);
//            return Result.error("操作异常");
//        }
//    }

    /**
     * @Author ysj闫世杰
     * @Description 单位移除菜单
     * @Date 2022/6/28 16:57
     * @Param: orgMenus
     * @return com.lingxu.base.common.api.vo.Result
     **/
//    @Override
//    public Result removeOrgMenu(List<SysOrgMenu> orgMenus) {
//        try {
//            for(SysOrgMenu s : orgMenus){
//                sysOrgMenuMapper.removeRoleMenu(s);
//            }
//            return Result.ok("操作成功");
//        }catch (Exception e){
//            logger.error("操作异常",e);
//            return Result.error("操作异常");
//        }
//    }

    /**
     * @Author ysj闫世杰
     * @Description
     * @Date 2022/6/30 16:57
     * @Param: userId
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @Override
    public Result findNotExistsByUser(String userId) {
        try {
            List<MenuTree> treeNodeList = new ArrayList<>();
            List<MenuTree> rootMenu= sysUserMenuMapper.findUserNotExistMenu(userId);
            TreeNodeUtil treeUtil=new TreeNodeUtil<>();
            treeNodeList.addAll(rootMenu);
            List<MenuTree> result=treeUtil.list2TreeNode(treeNodeList);
            return Result.ok(result,"查询成功");
        }catch (Exception e){
            logger.error("数据查询异常",e);
            return Result.error("查询异常");
        }
    }

    /**
     * @Author ysj闫世杰
     * @Description
     * @Date 2022/6/30 16:57
     * @Param: userId
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @Override
    public Result findExistsByUser(String userId) {
        try {
            List<MenuTree> treeNodeList = new ArrayList<>();
            List<MenuTree> rootMenu= sysUserMenuMapper.findUserExistMenu(userId);
            TreeNodeUtil treeUtil=new TreeNodeUtil<>();
            treeNodeList.addAll(rootMenu);
            List<MenuTree> result=treeUtil.list2TreeNode(treeNodeList);
            return Result.ok(result,"查询成功");
        }catch (Exception e){
            logger.error("数据查询异常",e);
            return Result.error("查询异常");
        }
    }

    /**
     * @Author ysj闫世杰
     * @Description
     * @Date 2022/6/30 16:57
     * @Param: userId
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @Override
    public Result addUserBindMenu(List<SysUserMenu> sysUserMenu) {
        try {
            Date date = new Date();
            for(SysUserMenu s : sysUserMenu){
                s.setId(UUID32Generator.generate());
                s.setCreateTime(date);
                s.setUpdateTime(date);
                s.setUpdateBy(s.getCreateBy());
                //查询是否已存在菜单
                int i = sysUserMenuMapper.findParentExistMenuByUserId(s.getUserId(),s.getMenuId());
                if(i > 0){
                    continue;
                }
                sysUserMenuMapper.insert(s);
            }
            return Result.ok("操作成功");
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.error("操作异常");
        }
    }

    /**
     * @Author ysj闫世杰
     * @Description
     * @Date 2022/6/30 16:57
     * @Param: userId
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @Override
    public Result removeUserMenu(List<SysUserMenu> sysUserMenu) {
        try {
            for(SysUserMenu s : sysUserMenu){
                sysUserMenuMapper.removeUserMenu(s);
            }
            return Result.ok("操作成功");
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.error("操作异常");
        }
    }


    /**
     * @Author ysj闫世杰
     * @Description 查询用户菜单下的按钮权限
     * @Date 2022/7/4 9:46
     * @Param: userId
     * @Param: menuId
     * @return com.lingxu.base.common.api.vo.Result
     **/
    @Override
    public Result findUserMenuButton(String userId, String menuId) {
        try {
            return Result.ok(sysUserMenuMapper.findUserMenuButton(userId,menuId),"查询成功");
        }catch (Exception e){
            logger.error("数据查询异常",e);
            return Result.error("查询异常");
        }
    }
}
